package com.panshi.config;

import com.panshi.bean.FieldError;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 全局公共异常帮助类
 *
 * @author  weiminke
 * @version 2020年5月7日
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 系统异常处理，500错误
     */
    @ExceptionHandler(value = {Exception.class,Throwable.class})
    public Object exceptionHandler(Throwable e,HttpServletRequest req,HttpServletResponse response) throws Exception {
        log.error("请求URI={}，系统内部异常={}",req.getRequestURI(),e);
        //String msg=e.getLocalizedMessage();

//    	if(AjaxUtils.isAjaxReq(req)){
//    		AjaxUtils.respJsonWrite(ResponseDataUtils.errorInternal(), response);
//    		return null;
//    	}else{
//    		return new ModelAndView("error/500");
//    	}
        return e;
    }

    /**
     * 业务自定义系统异常处理
     */
    //@ExceptionHandler(value = {ServiceException.class})
    public Object serviceExceptionHandler(Exception e,HttpServletRequest req) throws Exception {
        String msg=e.getMessage();
        log.error("请求URI={}，业务自定义系统异常信息={}",req.getRequestURI(), msg);

//		if(AjaxUtils.isAjaxReq(req)){
//			return ResponseDataUtils.fail(msg);
//		}else if(StatusCodeEnum.CODE_4040.getCode().equals(e.getCode())){
//			ModelAndView modelAndView=new ModelAndView("error/404");
//			modelAndView.setStatus(HttpStatus.NOT_FOUND);
//			return modelAndView;
//		}else{
//			return new ModelAndView("error/260");
//		}
        return true;
    }

    /**
     * 校验参数异常，显示输出具体字段错误列表
     */
    @ExceptionHandler(value=BindException.class)
    public Object validatorbindException(BindException e,HttpServletRequest req){
//		if(!AjaxUtils.isAjaxReq(req)){
//			return new ModelAndView("error/460");
//		}
        List<FieldError> fieldErrorVos=new ArrayList<FieldError>();
        StringBuilder msg=new StringBuilder();
        try {
            BindingResult bindingResult=e.getBindingResult();
            if(bindingResult.hasErrors()) {
                List<org.springframework.validation.FieldError> fieldErrors=bindingResult.getFieldErrors();

                FieldError fieldErrorVo;
                for (org.springframework.validation.FieldError MyfieldError:fieldErrors) {
                    fieldErrorVo=new FieldError(MyfieldError.getField(),MyfieldError.getDefaultMessage());
                    fieldErrorVos.add(fieldErrorVo);
                    msg.append(", ").append(MyfieldError.getDefaultMessage());
                }
                msg.deleteCharAt(0);
            }
        } catch (Throwable  tx) {
            log.error("转换校验参数异常",tx);
        }
        return fieldErrorVos;
    }



}